package com.tfjybj.itoo.exam.provider.service.impl;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Reference;;
import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.tfjybj.itoo.exam.entity.RoomEntity;
import com.tfjybj.itoo.exam.model.ExamRoomModel;
import com.tfjybj.itoo.exam.model.RoomModel;
import com.tfjybj.itoo.exam.provider.dao.RoomDao;
import com.tfjybj.itoo.exam.provider.service.BuildingService;
import com.tfjybj.itoo.exam.provider.service.ExamRoomService;
import com.tfjybj.itoo.exam.provider.service.RoomService;
import com.tfjybj.itoo.teach.FacadeApi.RoomApi;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * RoomService接口实现类
 * ${base}表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 10:35:56
 */
@Service("roomService")
public class RoomServiceImpl extends BaseServicePlusImpl<RoomDao, RoomEntity> implements RoomService {

    //region 模板生成

    @Resource
    private RoomDao roomDao;

    @Resource
    private BuildingService buildingService;
    @Resource
    private ExamRoomService examRoomService;
    @DubboReference(check = false)
    private RoomApi roomApi;


    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    @Override
    public ItooResult findByBuildingId(String buildingId) {
        return roomApi.findByBuildingId(buildingId);
    }

    @Override
    public ItooResult findByRoomId(String roomId) {
        return roomApi.findByRoomId(roomId);
    }

    @Override
    public ItooResult findComputerRoom(String schoolBranch) {
        return roomApi.findComputerRoom(schoolBranch);
    }

    /**
     * 根据roomId查找Room表所有信息
     *
     * @param pageNo     页码
     * @param pageSize   每页条数
     * @param searchInfo 查询参数
     * @return roomEntityList
     * @author 杨江涛
     * @since 2.0.0 2018-12-6 19:17:44
     */
    @Override
    public ItooResult findRoom(String searchInfo, Integer pageNo, Integer pageSize) {
        return roomApi.findRoom(searchInfo, pageNo, pageSize);
    }

    /**
     * 根据校区号和开考时间筛选教师
     *
     * @param schoolCode 校区号
     * @param startTime  配置考试开始时间
     * @author 马莹
     * @since 2018-12-7 20:53:31
     */
    @Override
    public ItooResult queryNoExamRoom(String schoolCode, String startTime) {
        return roomApi.queryNoExamRoom(schoolCode, startTime);
    }

    /**
     * 根据搜索框模糊查询考场
     *
     * @param fuzzyValue 搜索框参数
     * @param schoolKey  校区号
     * @param startTime  考试开始时间
     * @param endTime    考试结束时间
     * @return 模糊查询的结果集
     */
    @Override
    public List<RoomModel> selNotExamRoom(String fuzzyValue, String schoolKey, String startTime, String endTime) {
        //todo 调用教务接口
        List<RoomModel> roomModels = roomDao.selNotRoom(fuzzyValue, schoolKey, startTime, endTime);
        List<ExamRoomModel> examRoomModelList = examRoomService.selectSurplusRoom(fuzzyValue, schoolKey, startTime, endTime);
        if (examRoomModelList.size() > 0) {
            examRoomModelList.forEach(examRoomModel -> {
                RoomModel roomModel = new RoomModel();
                roomModel
                        .setId(examRoomModel.getRoomId())
                        .setName(examRoomModel.getName())
                        .setRoomCapacity(examRoomModel.getEmployCapacity());
                roomModels.add(roomModel);
            });
        }

        return roomModels;

    }


    /**
     * 根据东西校区以及考试开始时间加载考场
     *
     * @param schoolKey 校区key
     * @param startTime 考试开始时间
     * @param endTime   考试结束时间
     * @return 考场集合
     * @since 2019-3-24 17:33:12
     */
    @Override
    public List<RoomModel> selNotRoom(String schoolKey, String startTime, String endTime) {
        /**
         * 分成两部分查找,减少考场座位的浪费
         * 1.调取教务的room表查询没有安排的所有考场
         * 2.查询examRoom表中没有安排完座位的考场,(还有剩余座位的考场)
         */
        // 查询所有没有安排过的考场
        List<RoomModel> roomModelList = roomDao.selNotRoom("", schoolKey, startTime, endTime);
        // 查询安排过但是还有空闲座位的考场
        List<ExamRoomModel> examRoomModelList = examRoomService.selectSurplusRoom("", schoolKey, startTime, endTime);
        if (examRoomModelList.size() > 0) {
            examRoomModelList.forEach(examRoomModel -> {
                RoomModel roomModel = new RoomModel();
                roomModel.setId(examRoomModel.getRoomId());
                roomModel.setName(examRoomModel.getName());
                roomModel.setRoomCapacity(examRoomModel.getEmployCapacity());
                roomModelList.add(roomModel);
            });
        }
        return roomModelList;
    }
}
